package com.teapos.service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.teapos.constant.Global;
import com.teapos.dao.ConfigDao;
import com.teapos.dao.ProductDao;
import com.teapos.dao.QRcodeDao;
import com.teapos.util.Lists;
import com.teapos.util.Maps;
import com.teapos.util.Tool;

/**
 * 二维码service层
 * @author kangrui
 *
 */
@SuppressWarnings({"rawtypes", "unchecked"})
@Service
public class ScatService {
	
	@Autowired
	private QRcodeDao qRcodeDao;
	@Autowired
	private ConfigDao configDao;
	@Autowired
	private ProductDao productDao;
	
	/**
	 * 找到二维码对应商品
	 * @param code
	 * @return
	 */
	public Map findQRcode(String code, int role) {
		//System.err.println(code);
		Map map = qRcodeDao.findQRcodeById(code);
		//System.err.println(map);
		if(map!=null) {
			afterQuery(map);
			List discounts = configDao.getDepartmentDiscounts(role);
			map.put("discounts", discounts);
		}
		//System.err.println(map);
		return map;
	}
	
	public Map findQRcode(String code) {
		return qRcodeDao.findQRcodeById(code);
	}
	
	public List findTop10Products(String info) {
		/*List products = productDao.findProductVague("%"+info+"%");
		Iterator iter = products.iterator();
		List list = Lists.newArrayList();
		while(iter.hasNext()) {
			Map product = (Map) iter.next();
			Object pInfo = product.get("productionyear")+"-"+product.get("name");
			list.add(Maps.newHashMap("label", pInfo, 
					"value", pInfo, "id", product.get("id")));
		}*/
		List products = productDao.findProductWithUnit("%"+info+"%");
		return products;
	}

	public Map findQRcodeByProductId(Map param) {
		System.err.println(param);
		param.put("unit", Tool.toInt(param.get("unit")));
		Map qRcode = qRcodeDao.findQrCodeByPid(param);
		//System.err.println(qRcode);
		if(qRcode!=null) {
			afterQuery(qRcode);
			List discounts = configDao.getDepartmentDiscounts(Tool.toInt(param.get("role")));
			System.err.println(discounts);
			qRcode.put("discounts", discounts);
		}
		return qRcode;
	}
	
	private void afterQuery(Map map) {
		//String unit = Tool.toString(map.get("aunit"));
		//map.put("type_display", Unit4SaveEnum.getEnumByName(unit).getDesc());
		int unit = Tool.toInt(map.get("unit_type"));
		
		if(unit==Global.PIAN) {
			//map.put("type_display", "散装");
			map.put("price", Tool.toInt(map.get("dpxsj"))/100);
		} else if(unit==Global.JIAN) {
			//map.put("type_display", "整装");
			map.put("price", Tool.toInt(map.get("zjxsj"))/100);
		} else if(unit==Global.TONG) {
			int spid = Tool.toInt(map.get("spid"));
			Map spec = qRcodeDao.findSpecBySpId(spid);
			int num = Tool.toInt(spec.get("middle_amount"));
			map.put("price", Tool.toInt(map.get("dpxsj"))/100*num);
		}
		//map.put("price_display", Tool.formatMoney(Tool.toLong(map.get("price"))));
		//map.put("discounts", configDao.getDepartmentDiscounts(departmentId));
//		List array = Lists.newArrayList(Maps.newHashMap("value", 0.99), Maps.newHashMap("value", 0.98)
//				, Maps.newHashMap("value", 0.97), Maps.newHashMap("value", 0.96)
//				, Maps.newHashMap("value", 0.95), Maps.newHashMap("value", 0.94)
//				, Maps.newHashMap("value", 0.93), Maps.newHashMap("value", 0.92)
//				, Maps.newHashMap("value", 0.91), Maps.newHashMap("value", 0.9));
		//System.err.println(Lists.newArrayList(1,2,3));
		//System.err.println(Maps.newHashMap("1", 1));
//		map.put("discounts", array);
	}

	public String findTempCode(String id) {
		return qRcodeDao.findTempCodeByStoreId(id);
	}

	public List findUnitByPid(String id) {
		Map map = productDao.findUnitByPid(id);
		return Lists.newArrayList(map.get("unit"), map.get("bigunit"), map.get("middle_unit"));
	}

}
